package com.example.location;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

import android.widget.Button;
import android.widget.Toast;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.InfoWindow;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.map.InfoWindow.OnInfoWindowClickListener;
import com.baidu.mapapi.map.MarkerOptions.MarkerAnimateType;
import com.baidu.mapapi.model.LatLng;
import com.example.baidumapsum.R;
import com.example.bean.LocationBean;
import com.example.bean.Rows;
import com.google.gson.Gson;





/**
 * @ClassName: 
 * @Description:显示多个坐标点
 * @author gongxibo 
 * @date 创建时间：2015年12月20日 下午4:15:33 
 */
public class MultiLocationActivity extends ShowMylocationActivity{

	BitmapDescriptor bitmap = BitmapDescriptorFactory
			.fromResource(R.drawable.icon_gcoding);
	HashMap<Marker, Rows> map = new HashMap<Marker, Rows>();
	private InfoWindow mInfoWindow;
	@Override
	protected void initView() {
		super.initView();
		paesrData();
		
	}
     /**
      * 解析json数据
      */
	private void paesrData() {
		Gson gson = new Gson();
		LocationBean data = gson.fromJson(getJson(), LocationBean.class);
		List<Rows> dataList = data.getRows();
		for (Rows bean : dataList) {
			makerOverlay(bean);
		}
	}
		/**
		 * 在地图上画图标
		 * 
		 * @param bean
		 */
		private void makerOverlay(Rows bean) {
			double lat = Double.valueOf(bean.getArea_lat());
			double lon = Double.valueOf(bean.getArea_lon());
			// 定义Maker坐标点
			LatLng point = new LatLng(lat, lon);
			// 构建Marker图标

			// 构建MarkerOption，用于在地图上添加Marker
			MarkerOptions option = new MarkerOptions().position(point).icon(bitmap);
			// 掉下动画
			option.animateType(MarkerAnimateType.drop);
			// 在地图上添加Marker，并显示
			Overlay overLay = mBaiduMap.addOverlay(option);
			Marker mMarkerA = (Marker) (overLay);
			map.put(mMarkerA, bean);

		}
	/**
	 * 得到json数据
	 */
	private String getJson() {
		InputStream inputStream = getResources().openRawResource(R.raw.locations);
		@SuppressWarnings("resource")
		String json = new Scanner(inputStream).useDelimiter("\\A").next();
		return json;
	}
	@Override
	protected void initListener() {
		// 点击泡泡
		mBaiduMap
				.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
					@Override
					public boolean onMarkerClick(Marker marker) {
						clickMarker(marker);
						return true;
					}
				});
	}
	/**
	 * 点击marker事件
	 * 
	 * @param marker
	 */
	private void clickMarker(Marker marker) {
		Button button = new Button(getApplicationContext());
		button.setBackgroundResource(R.drawable.popup);
		OnInfoWindowClickListener listener = null;
		Set<Marker> set = map.keySet();
		for (Marker mark : set) {
			if (mark == marker) {
				final Rows data = map.get(marker);
				button.setText(data.getTitle());
				button.setTextColor(getResources().getColor(
						android.R.color.holo_red_dark));
				listener = new OnInfoWindowClickListener() {
					@Override
					public void onInfoWindowClick() {
						// TODO Auto-generated method stub
						mBaiduMap.hideInfoWindow();
						Toast.makeText(context, "点击了 id = "+data.getId()+" 的数据", 
								Toast.LENGTH_SHORT).show();
					}

				};
				break;
			}
		}
		LatLng ll = marker.getPosition();
		mInfoWindow = new InfoWindow(BitmapDescriptorFactory.fromView(button),
				ll, -47, listener);
		mBaiduMap.showInfoWindow(mInfoWindow);
	}

}
